﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports DevExpress.Utils

Public Class FormRk
    Dim en As entity_cplh_ck
    Dim objCA As New clsCPLHCK
    Dim objProdInfo As New clsProdInfo
    Dim objPgdMes As New clsPgdMes
    Dim httpHelper As New HttpHelper
    Public usr As String
    Dim warehousecode As String
    Private Sub FormRk_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        setEmptyLOC()
        txtProdId.Focus()
    End Sub

    Private Sub setEmptyLOC()
        Dim objMstList As New clsMstList
        Dim dt As DataTable
        loc.Items.Clear()
        dt = objMstList.getEmptyLoc("4")
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                loc.Items.Add(dt.Rows(i)("mst_value"))
            Next
        End If
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Search()
    End Sub

    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If e.KeyChar = vbCr Then
            Search()
        End If
    End Sub

    Private Sub Search()
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            Dim dtt As DataTable = objProdInfo.selectProdInfo(txtProdId.Text.Trim) '还需增加查询条件
            If dtt.Rows.Count > 0 Then
                Dim dr As DataRow = dtt.Rows(0)
                txt_prod_spec.Text = dr("prod_spec_id")
                txt_prod_spec.Text = dr("prod_spec_name")
            Else
                MsgBox("未检索到此生产编号", vbYes, "提示")
            End If
        End If
    End Sub

    Private Sub btnRK_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        'Dim r As String = mesRk("1", "ZAGZ07", "02.02.009", "接口入库测试428-1")
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) Then
            MsgBox("请扫描或输入标签号", vbYes, "提示")
            Exit Sub
        End If

        Dim dtxt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim(), "8")

        If dtxt.Rows.Count > 0 Then
            Dim dtt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim())
            If dtt.Rows.Count = 0 Then
                'Dim dtpgdmeslh As DataTable = objPgdMes.getListBySysId1(dtxt.Rows(0)("b_pgd_lh_details_sys_id").ToString())
                'setEN(dtpgdmeslh.Rows(0)("b_pgd_mes_sys_id").ToString())
                setEN(dtxt.Rows(0)("pgd_id").ToString())
                If objCA.intoCK(en) = -2 Then
                    MsgBox("入库失败", vbYes, "提示")
                    Exit Sub
                Else
                    clear()
                    MsgBox("入库成功"， vbYes, "提示")
                    'Dim dtPgdMes As DataTable = objPgdMes.getListBySysId(dtpgdmeslh.Rows(0)("b_pgd_mes_sys_id").ToString())
                    'Dim rtnMesRK As Integer = mesRk("1", warehousecode,
                    '                                dtPgdMes.Rows(0)("type_value").ToString(),
                    '                                dtPgdMes.Rows(0)("mes_project_code").ToString())
                    'If rtnMesRK.Equals(1) Then
                    '    clear()
                    '    MsgBox("入库成功，MES入库成功"， vbYes, "提示")
                    'Else
                    '    MsgBox("入库成功，MES入库失败"， vbYes, "提示")
                    'End If
                End If
            Else
                MsgBox("该生产编号已入库或出库，请核查"， vbYes, "提示")
            End If
        Else
            Dim dtProdInfo As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim())
            If dtProdInfo.Rows.Count > 0 Then
                If dtProdInfo.Rows(0)("status").ToString().Equals("5") Then
                    MsgBox("此生产编号当前状态为：硫化中"， vbYes, "提示")
                End If
                If dtProdInfo.Rows(0)("status").ToString().Equals("6") Then
                    MsgBox("此生产编号当前状态为：硫化完"， vbYes, "提示")
                End If
                If dtProdInfo.Rows(0)("status").ToString().Equals("7") Then
                    MsgBox("此生产编号当前状态为：脱模完"， vbYes, "提示")
                End If
                If dtProdInfo.Rows(0)("status").ToString().Equals("8") Then
                    MsgBox("此生产编号当前状态为：修边完"， vbYes, "提示")
                End If
                If dtProdInfo.Rows(0)("status").ToString().Equals("9") Then
                    MsgBox("此生产编号当前状态为：硫化入库"， vbYes, "提示")
                End If
                If dtProdInfo.Rows(0)("status").ToString().Equals("10") Then
                    MsgBox("此生产编号当前状态为：硫化出库"， vbYes, "提示")
                End If
            End If
            'MsgBox("未查询到修边完成的此生产编号信息"， vbYes, "提示")
        End If


    End Sub

    Public Sub clear()
        txtProdId.Text = String.Empty
        txt_prod_spec.Text = String.Empty
        loc.Text = String.Empty
        setEmptyLOC()
        txtProdId.Focus()
    End Sub
    Public Sub setEN(b_pgd_mes_sys_id As String)
        en = New entity_cplh_ck
        With en
            .factory_id = "TS"
            .prod_id = txtProdId.Text.Trim()
            .wh_no = loc.Text.Trim()
            .b_pgd_mes_sys_id = b_pgd_mes_sys_id
            .into_ope = usr
            .status = "0"
        End With
    End Sub

    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As Integer
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "硫化" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103104""" '部门id
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("入库成功") Then
            rtn_int = 1
        End If
        Return rtn_int
    End Function


End Class


Public Class entity_cplh_ck
    Public sys_id As String
    Public factory_id As String
    Public wh_no As String
    Public prod_id As String
    Public b_pgd_mes_sys_id As String
    Public into_ope As String
    Public status As String
End Class